package com.lsh.unit;

import com.lsh.exception.DelFastDFSFileException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class FastDFSUtil {

	public static StorageClient1 getStorageClient() throws IOException, MyException {
		ClassPathResource classPathResource = new ClassPathResource("fastdfs-client.properties");
		InputStream inputStream = classPathResource.getInputStream();
		File fastdfsfile = new File("/data/fastdfs/fastdfs-client.properties");
		FileUtils.copyInputStreamToFile(inputStream,fastdfsfile);
		log.info("fastdfs配置文件："+fastdfsfile.getAbsolutePath());
		ClientGlobal.init("/data/fastdfs/fastdfs-client.properties");
		// 2. 创建管理端对象
		TrackerClient trackerClient = new TrackerClient();
		// 3. 通过管理端对象获取连接
		TrackerServer connection = trackerClient.getConnection();
		// 4. 创建存储端对象
		StorageClient1 storageClient = new StorageClient1(connection, null);
		return storageClient;
	}
	public static StorageClient1 getStorageClient2() throws IOException, MyException {
		String filePath = new ClassPathResource("fastdfs-client.properties").getFile().getPath();
		ClientGlobal.init(filePath);
		// 2. 创建管理端对象
		TrackerClient trackerClient = new TrackerClient();
		// 3. 通过管理端对象获取连接
		TrackerServer connection = trackerClient.getConnection();
		// 4. 创建存储端对象
		StorageClient1 storageClient = new StorageClient1(connection, null);
		return storageClient;
	}

	public static String upload(MultipartFile file) throws Exception{
		log.info("开始上传!");
		// 获得文件名
		String fileName = file.getOriginalFilename();
		// 获得文件后缀
		String[] split = fileName.split("\\.");
		String suffix = split[1];
		log.info("FastDFSUtil : 文件名 : "+fileName+" , 后缀 : "+suffix);
		// 1. 加载配置文件    获取resources 目录下的文件
		//本地测试可以使用这种方法
//		String filePath = new ClassPathResource("fastdfs-client.properties").getFile().getPath();
//		ClientGlobal.init(filePath);
		//2. 打成jar包后
//		ClassPathResource classPathResource = new ClassPathResource("fastdfs-client.properties");
//		InputStream inputStream = classPathResource.getInputStream();
//		File fastdfsfile = new File("/data/FastDFS/fastdfs-client.properties");
//		FileUtils.copyInputStreamToFile(inputStream,fastdfsfile);
//		log.info("fastdfs配置文件："+fastdfsfile.getAbsolutePath());
//		ClientGlobal.init("/data/FastDFS/fastdfs-client.properties");
		// 2. 创建管理端对象
//		TrackerClient trackerClient = new TrackerClient();
		// 3. 通过管理端对象获取连接
//		TrackerServer connection = trackerClient.getConnection();
		// 4. 创建存储端对象
//		StorageClient1 storageClient = new StorageClient1(connection, null);

		StorageClient1 storageClient = FastDFSUtil.getStorageClient();
//		StorageClient1 storageClient = FastDFSUtil.getStorageClient2();
		NameValuePair[] meta_list = new NameValuePair[1];
		// 需要传入文件名
		meta_list[0] = new NameValuePair("fileName", fileName);

		// 5. 上传文件
		/*
		 * 参数1:要上传的文件的字节数组
		 * 参数2:要上传的文件类型
		 * 参数3:文件属性信息对象数组
		 * 返回值 : 存储在Storage中的地址
		 */
		String path = storageClient.upload_file1(file.getBytes(), suffix, meta_list);
		log.info("FastDFSUtil -上传返回的文件id是   :   "+ path);
		return path;
	}
	
	
	public static byte[] download(String fid) throws IOException, MyException {
		log.info("接收到的文件ID: " + fid);
		StorageClient1 storageClient = FastDFSUtil.getStorageClient();
//		StorageClient1 storageClient = FastDFSUtil.getStorageClient2();

		// 5.通过文件id得到文件的字节数组
		byte[] bytes = storageClient.download_file1(fid);
		return bytes;
	}

	/**
	 *
	 * @param fid
	 * @throws IOException
	 * @throws MyException
	 * -1失败,0成功
	 */
	public static void delFastDFSFile(String fid) throws IOException, MyException {
		StorageClient1 storageClient = FastDFSUtil.getStorageClient();
//		StorageClient1 storageClient = FastDFSUtil.getStorageClient2();

		int i = storageClient.delete_file1(fid);
		if (i==0){
			log.info(fid+"删除成功");
		}else {
			throw new DelFastDFSFileException(501,fid+"文件删除失败");
		}
	}
	
}
